<?php


namespace app\job;

use app\admin\model\Equity;
use app\admin\model\Order;
use app\admin\model\User;
use app\admin\model\UserRelation;
use think\facade\Log;
use think\queue\Job;
use app\service\FXApiService;
use app\service\IyunBinkService;

class EquityJob
{
    /**
     *分润
     */
    public function fire(Job $job, $data)
    {
        $isJobDone = $this->doTask($data);
        if ($isJobDone) {
            $job->delete();
        } else {
            $job->release(2);
        }
    }

    /**
     *
     * task具体执行逻辑
     */
    public function doTask($data)
    {
        $orderModel = new Order();
        $orderFind = $orderModel->where('id', $data['order_id'])->find();
        //$res = Db::table('chd_auth_role')->insert($params);
        Log::info("==分润任务执行==" . json_encode($data));
        if ($orderFind) {
            $UserRelationModel = new UserRelation();

            switch ((int)$orderFind->type) {
                case 1:
                    # 凤栖..
                    // $phone = FXApiService::getMemberInfo($orderFind->mechanId);
                
                    // if ($phone['status'] == '1') {
                        // if (isset($phone['data'])) {
                            $user = $UserRelationModel->where('t_id', $orderFind->mechanId)->find();
                            if ($user) {
                                $orderFind->user_id = $user->user_id;
                            }
                    //     }else{
                    //         Log::info("==凤栖分润任务错误1==" . json_encode($phone));
                    //         return false;
                    //     }
                    // }else{
                    //     Log::info("==凤栖分润任务错误2==" . json_encode($phone));
                    //     return false;
                    // }
                    $yongjin = FXApiService::queryProductCommissons($orderFind->ware_id);
                    if ($yongjin['status'] == '1') {
                        $orderFind->creat_date_end = time();
                        $orderFind->commission = round($orderFind->totalPremium * $yongjin['data'][0]['commission'], 2);
                    }else{
                        Log::info("==凤栖分润任务错误2==" . json_encode($yongjin));
                        $orderFind->commission =round($orderFind->totalPremium * 0.02, 2);
                        // return false;
                    }

                    break;
                case 2:
                    # I云服...
                    $user = $UserRelationModel->where('t_id', $orderFind->commission_owner)->find();
                    if ($user) {
                        $orderFind->user_id = $user->user_id;
                    }
                    $yongjin = IyunBinkService::querySpreadRate($orderFind->commission_owner, $orderFind->policy_no);
                    if ($yongjin['status'] == '1') {
                        $orderFind->creat_date_end = $yongjin['data']['result']['creatDateEnd'];
                        $orderFind->effective_date = $yongjin['data']['result']['effectiveDate'];
                        $orderFind->commission = $yongjin['data']['result']['commission'];
                        $orderFind->seller = $yongjin['data']['result']['seller'];
                    }else{
                        Log::info("==i云服分润任务错误2==" . json_encode($yongjin));
                        $orderFind->commission =round($orderFind->totalPremium * 0.02, 2);
                        // return false;
                    }
                    break;
                default:
                    Log::info("==分润任务错误==" . json_encode($data));
                    return false;
                    break;
            }
            $orderFind->save();
            $eModel = new Equity();
            $eFind=$eModel->where('user_id',$user->user_id)->where('order_id',$data['order_id'])->find();
            if($eFind){
                return true;
            }
            $arr = [
                'user_id' => $user->user_id,
                'order_id' => $data['order_id'],
                'order_fee' => $orderFind->commission,
                'unit_percentage' => 100,
                'fee' => $orderFind->commission,
                'prime_price' => $orderFind->totalPremium,
                'status' => '1',
                'channel' => $orderFind->type,
                'date' => date('Ymd', strtotime("+15 day")),
                'createtime' => time()
            ];
            $e = $eModel->create($arr);
            if ($e) {
                $userFind = User::where('id', $user->user_id)->find();
                $userFind->hlat_money += $orderFind->commission;
                $userFind->save();
                if (isset($userFind->rec_user_id)) {
                    $userrec = User::where('id', $userFind->rec_user_id)->find();
                    $eFind=$eModel->where('user_id',$userFind->rec_user_id)->where('order_id',$data['order_id'])->find();
                    if($eFind){
                        return true;
                    }
                    if ($userrec) {
                        $arr['user_id'] = $userFind->rec_user_id;
                        $arr['unit_percentage'] = 2;
                        $arr['fee'] = round($orderFind->commission * 0.02, 2);
                        $e = $eModel->create($arr);
                        $userrec->hlat_money += round($orderFind->commission * 0.02, 2);
                        $userrec->save();
                    }
                }
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}
